#import "RNListDetailHeader.h"
#import "RNCategory.h"
#import "RNAisle.h"
#import "RNConstants.h"

@implementation RNListDetailHeader

+(NSArray *)arrayOfHeadersWithRootObject:(id)rootObject filteredWithObject:(id)filterObject inManagedObjectContext:(NSManagedObjectContext *)moc {
    RNListDetailHeader *header1 = [[self alloc] initWithRootObject:rootObject];
    [header1 setHeaderHeight:RNHiddenHeaderHeight];
    NSString *selectedKeyPath1 = nil;
    
    RNListDetailHeader *header2 = [[self alloc] initWithRootObject:rootObject];
    [header2 setHeaderHeight:RNHeaderHeight];
    [header2 setKeyPath:@"items"];
    [header2 setShowsTitle:YES];
    NSPredicate *predicate2 = nil;
    
    if (filterObject) {
        [[header1 primaryController] setFilterPredicate:[NSPredicate predicateWithFormat:@"store == %@", filterObject]];
        [RNAisle setCurrentListObject:rootObject];
        [RNAisle setShowsChildren:YES];
        selectedKeyPath1 = @"items.@distinctUnionOfSets.aisles";
        
        [header2 setTitleTemplate:@"with no aisle assigned"];
        predicate2 = [NSPredicate predicateWithFormat:@"NOT %@ IN stores", filterObject];
    }
    else {
        [RNCategory setShowsChildren:YES];
        [RNCategory filterWithObject:rootObject];
        selectedKeyPath1 = @"items.@distinctUnionOfObjects.category";
        
        predicate2 = [NSPredicate predicateWithFormat:@"category == nil"];
        [header2 setTitleTemplate:@"with no category assigned"];
    }
    [header1 setKeyPath:selectedKeyPath1];
    [[header2 primaryController] setFilterPredicate:predicate2];
    return [NSArray arrayWithObjects:header1, header2, nil];
}

@end
